import numpy as np
import matplotlib.pyplot as plt
from typing import List, Dict, Any

plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['font.sans-serif'] = ['SimHei', 'Arial', 'sans-serif']

#弯度 - 第一重分段积分
def M(x):
    return np.piecewise(x,
                        [x <= 0.5, x > 0.5],
                        [lambda x: (2*0.0015*x - 2*0.00075),
                         lambda x: (-2*0.0015*x + 2*0.00075)])

#转角 - 第二重积分
def A(x):
    return np.piecewise(x,
                        [x <= 0.5, x > 0.5],
                        [lambda x: 0.0015*x**2 - 2*0.00075*x,
                         lambda x: -(-0.0015*(x)**2 + 2*0.00075*(x))])

#挠度 - 形变
def D(x):
    return np.piecewise(x,
                        [x <= 0.5, x > 0.5],
                        [lambda x: -(0.0005*(x-0.5)**3 - 0.00075*(x-0.5)**2),
                         lambda x: -(-(0.0005*(x-0.5)**3 + 0.00075*(x-0.5)**2))])

# 生成数据点
x1 = np.linspace(0, 0.5, 100)
x2 = np.linspace(0.5, 1, 100)

print(x2[-1])

y1 = M(x1)
y2 = M(x2)

y3 = A(x1)
y4 = A(x2)

y5 = D(x1)
y6 = D(x2)
'''
if y2[0] != y1[-1]:
    print(f'{y2[0]}, {y1[-1]}')
    offset = - (y2[0] - y1[0])
    y2 = [item + offset for item in y2]
'''

# 绘图
# 创建主坐标轴
fig, ax1 = plt.subplots(figsize=(10, 6))
lines = []
line, = ax1.plot(x1, y1, 'r-', linewidth=2, label='弯矩M [0, 0.5]')
lines.append(line)
line, = ax1.plot(x2, y2, 'r-', linewidth=2, label='弯矩M [0.5, 1]')
lines.append(line)
ax1.tick_params(axis='y', labelcolor='r', labelleft='S')  # 左侧刻度颜色

ax2 = ax1.twinx()
line, =  ax2.plot(x1, y3, 'g-', linewidth=2, label='转角A [0, 0.5]')
lines.append(line)
line, = ax2.plot(x2, y4, 'g-', linewidth=2, label='转角A [0.5, 1]')
lines.append(line)

ax3 = ax1.twinx()
line, =  ax2.plot(x1, y5, 'b-', linewidth=2, label='挠度-形变 [0, 0.5]')
lines.append(line)
line, = ax2.plot(x2, y6, 'b-', linewidth=2, label='挠度-形变 [0.5, 1]')
lines.append(line)

ax2.tick_params(axis='y', labelcolor='b', labelright = 'T')  # 右侧刻度颜色

plt.scatter([0, 0.5, 1], [D(0), D(0.5), D(1)], c='black', zorder=5)  # 关键点
plt.text(0, D(0)+0.0001, 'D(0)=0 梁左侧支点', fontsize=12, ha='right')
plt.text(0.5, D(0.5)-0.0002, 'D(0.5)=0 外部应力施加位置点 ', fontsize=12, ha='center')
plt.text(1, D(1)+0.0001, 'D(1)=0 梁右侧支点', fontsize=12, ha='left')

# 设置标题和标签
plt.title('弯矩 - 转角 - 挠度 - 剪力集中载荷的三重积分(假定外部集中载荷中置)', fontsize=14)
plt.xlabel('x', fontsize=12)
#plt.ylabel('F(x) (Cumulative Integral)', fontsize=12)
plt.grid(linestyle='--', alpha=0.7)
plt.legend(handles=lines)
plt.show()